package com.ruoyi.points.pointsAccessDetail.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.points.pointsAccess.domain.PointsAccess;
import com.ruoyi.points.pointsAccess.service.IPointsAccessService;
import com.ruoyi.points.pointsManage.service.IPointsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.points.pointsAccessDetail.domain.PointsAccessDetail;
import com.ruoyi.points.pointsAccessDetail.service.IPointsAccessDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分详情Controller
 * 
 * @author ruoyi
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/points/pointsAccessDetail")
public class PointsAccessDetailController extends BaseController
{
    @Autowired
    private IPointsAccessDetailService pointsAccessDetailService;

    @Autowired
    private IPointsAccessService pointsAccessService;

    @Autowired
    private IPointsService iPointsService;

    /**
     * 查询积分详情列表
     */
//    @PreAuthorize("@ss.hasPermi('points:pointsAccessDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(PointsAccessDetail pointsAccessDetail)
    {
        startPage();
        List<PointsAccessDetail> list = pointsAccessDetailService.selectPointsAccessDetailList(pointsAccessDetail);
        return getDataTable(list);
    }

    /**
     * 导出积分详情列表
     */
    @PreAuthorize("@ss.hasPermi('points:pointsAccessDetail:export')")
    @Log(title = "积分详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PointsAccessDetail pointsAccessDetail)
    {
        List<PointsAccessDetail> list = pointsAccessDetailService.selectPointsAccessDetailList(pointsAccessDetail);
        ExcelUtil<PointsAccessDetail> util = new ExcelUtil<PointsAccessDetail>(PointsAccessDetail.class);
        util.exportExcel(response, list, "积分详情数据");
    }

    /**
     * 获取积分详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('points:pointsAccessDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointsAccessDetailService.selectPointsAccessDetailById(id));
    }

    /**
     * 新增积分详情
     * 1. 增加积分: userId, accessId
     * 2. 消费积分: userId, accessId = 0, quantity(小于0)
     */
    @PreAuthorize("@ss.hasPermi('points:pointsAccessDetail:add')")
    @Log(title = "积分详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointsAccessDetail pointsAccessDetail)
    {
        Long pointAccessId = pointsAccessDetail.getAccessId();
        boolean consume = pointAccessId == 0;

        if (!consume) {
            PointsAccess pointsAccess = pointsAccessService.selectPointsAccessById(pointAccessId);
            // 查找今天该用户通过该方式获取积分的次数
            int count = pointsAccessDetailService.selectCountById(pointsAccessDetail);
            // count小于0表示是消费积分
            System.out.println(count);
            System.out.println(pointsAccess.getCount());
            if (count >= pointsAccess.getCount()) {
                return AjaxResult.error("该获取方式已经达到今日上限");
            }
            iPointsService.increasePoints(pointsAccessDetail.getUserId(), pointsAccess.getQuantity());
            pointsAccessDetail.setQuantity(pointsAccess.getQuantity());
        } else {
            int result = iPointsService.increasePoints(pointsAccessDetail.getUserId(), pointsAccessDetail.getQuantity());
            if (result < 0) {
                return AjaxResult.error("您的积分余额不足,购买失败");
            }
        }
        pointsAccessDetail.setDate(new Date());
        return toAjax(pointsAccessDetailService.insertPointsAccessDetail(pointsAccessDetail));
    }

    /**
     * 修改积分详情
     */
    @PreAuthorize("@ss.hasPermi('points:pointsAccessDetail:edit')")
    @Log(title = "积分详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PointsAccessDetail pointsAccessDetail)
    {
        return toAjax(pointsAccessDetailService.updatePointsAccessDetail(pointsAccessDetail));
    }

    /**
     * 删除积分详情
     */
    @PreAuthorize("@ss.hasPermi('points:pointsAccessDetail:remove')")
    @Log(title = "积分详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pointsAccessDetailService.deletePointsAccessDetailByIds(ids));
    }
}
